﻿using GetData.webProductClass;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    public class ZhiZhiZhi
    {
        HtmlAgilityPack.HtmlDocument doc;
        public ZhiZhiZhi()
        {
            doc = new HtmlAgilityPack.HtmlDocument();

        }
        /// <summary>
        /// 值值值 HTML信息 获取商品信息 并保存到数据库
        /// </summary>
        /// <returns></returns>
        public string GetSaleURL(string url, int navType)
        {
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            var productList = GetProductByRegexToHtml(htmlText, navType);
            if (productList == null || productList.Count < 1)
            {
                return "网络异常，未能获取数据";
            }
            // GetProductList(jsonStr, productList);//得到要插入数据库的数据
            //保存数据库
            DataTable dt = null;
            //转化成table插入
            string errorMsg = "";
            dt = CustomMethod.ListToDataTable<Product>(productList, "Product", ref errorMsg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return errorMsg;
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            string msg = "";

            if (bll.SaveTableData("Product", dt, out msg))
            {
                return "";
            }
            else //返回失败信息
            {
                return msg;
            }
        }
        /// <summary>
        /// 根据正则解析商品信息
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="type">海购还是精选</param>
        /// <returns></returns>
        private List<Product> GetProductByRegexToHtml(string htmlText, int type)
        {
            List<Product> pList = new List<Product>();
            //string productRegex = @"<div class=""post_box"">[\s\S]*?<!--分享 end-->";
            doc.LoadHtml(htmlText);
            var collec = doc.DocumentNode.SelectNodes("//div[@class='post_box']");
            List<string> list = new List<string>(); ;
            //  list = CustomMethod.GetInfoByRegex(productRegex, htmlText, 0);
            if (collec == null || collec.Count < 1)
            {
                string msg = "值值值：GetProductByRegex ，没有抓到匹配的商品，检查匹配规则。";
                CustomMethod.WriteLog(msg);
                return null;
            }
            for (int index = 0; index < collec.Count; index++)
            {
                list.Add(collec[index].InnerHtml);
            }
            ProductTypeEnum pEnum = new ProductTypeEnum();
            //每个商品匹配所有相关属性并赋值
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                //product.FromWeb = "值值值";
                //product.TypeId = type;
                try
                {

                    product = GetProduct(list[index]);
                    if (product == null)
                    {
                        continue;
                    }
                    //    var herfList = CustomMethod.GetInfoFroma(list[index]);
                    //    if (herfList == null || herfList.Count < 7)
                    //    {
                    //        string msg = "值值值：没有获取到商品相关链接信息。";
                    //        CustomMethod.WriteLog(msg);
                    //        continue;
                    //    }
                    //    string buyUrl = "";
                    //    if (herfList[0].Contains("hizhizhi.com"))
                    //    {
                    //        buyUrl = herfList[0];
                    //    }
                    //    else
                    //    {
                    //        buyUrl = "http://zhizhizhi.com/" + herfList[0];
                    //    }
                    //    string productUrl = "";
                    //    product.Plink = CustomMethod.GetTrueUrl(buyUrl, ref productUrl);
                    //    if (product.Plink == "")
                    //    {
                    //        if (productUrl == "")
                    //        {
                    //            string msg = "值值值：没有获取到商品购买链接。";
                    //            CustomMethod.WriteLog(msg);
                    //        }
                    //        continue;
                    //    }
                    //    product.Memo = productUrl;
                    //    string linkRegex = @"<li class=""theshop"">(.*?)</li>";
                    //    var linkContent = CustomMethod.GetInfoByRegex(linkRegex, list[index], 1);
                    //    if (linkContent != null && linkContent.Count > 0)
                    //    {
                    //        var herLink = CustomMethod.GetInfoFroma(linkContent[0]);
                    //        if (herLink != null && herLink.Count > 0)
                    //        {
                    //            product.Source = herLink[1];
                    //        }
                    //    }
                    //    //  product.Source = herfList[herfList.Count - 7];
                    //    string detailUrl = herfList[2];
                    //    string titlePriceRegex = @"<span class=(.*?)>([^<]+)</span>";
                    //    var tiltePriceList = CustomMethod.GetInfoByRegex(titlePriceRegex, herfList[3], 2);
                    //    if (tiltePriceList == null || tiltePriceList.Count < 2)
                    //    {
                    //        string msg = "值值值：没有获取到商品名称，价格。";
                    //        CustomMethod.WriteLog(msg);
                    //        continue;
                    //    }
                    //    product.Pname = tiltePriceList[0];
                    //    product.Price = tiltePriceList[1];
                    //    //获取描述信息
                    //    string detailText = CustomMethod.GetHtmlByHttpHelper(detailUrl);
                    //    //-----------------------------------------获取商品分类---------------------------------------
                    //    string regex = @"<div class=""mianbaoxie"">[\s\S]*?</div>";
                    //    product.ProductTypeId = pEnum.GetProductTypeId(detailText, regex, typeof(ProductTypeEnum.zhizhizhiType), 0);

                    //    //-----------------------------------------------------------------------

                    //    string desRegex = @"<div class=""content_a"">[\s\S]*?</div>";
                    //    var desList = CustomMethod.GetInfoByRegex(desRegex, detailText, 0);
                    //    if (desList == null || desList.Count < 1)
                    //    {
                    //        string msg = "值值值：没有获取到商品描述，查看规则。";
                    //        CustomMethod.WriteLog(msg);
                    //        product.Description = product.Pname;
                    //    }
                    //    else
                    //    {
                    //        //string desContentRegex = @"<p>[\s\S]*?</p>";
                    //        //string desCont = "";
                    //        //var desContentList = CustomMethod.GetInfoByRegex(desContentRegex, desList[0], 0);
                    //        //for (int count = 0; count < desContentList.Count; count++)
                    //        //{
                    //        //    desCont += Regex.Replace(desContentList[count], @"<(.[^>]*)>", "") + "|||";
                    //        //}

                    //        //product.Description = desCont == "" ? product.Pname : desCont;
                    //    }
                    //    //图片链接
                    //    string imgUrl = CustomMethod.GetImgUrl(list[index], "值值值");
                    //    if (imgUrl == "")
                    //    {
                    //        continue;
                    //    }
                    //    product.ImgUrl = imgUrl;
                    //    product.UpdateTime = DateTime.Now;
                    //    product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //    product.Pname = System.Web.HttpUtility.HtmlEncode(product.Pname);

                    pList.Add(product);
                }

                catch (Exception ex)
                {
                    CustomMethod.WriteLog("GetProductByRegexToHtml:" + ex.Message.ToString() + index);
                    continue;
                }
            }
            var newList = CustomMethod.GetDistinctProdcuts(pList);
            newList.Reverse();
            return newList;
        }
        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "值值值";
            pro.TypeId = 1;
            pro.UpdateTime = DateTime.Now;
            pro.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            doc.LoadHtml(htmlContent);
            //名称
            var name = CustomMethod.GetValue(doc, "//h3/a");
            if (name == "")
            {
                return null;
            }
            pro.Pname = name.Trim().Replace("&nbsp;", "");
            //价格
            var pric = CustomMethod.GetValue(doc, "//h3/a/i[@class='dprice']");
            pro.Price = pric.Trim().Replace("&nbsp;", ""); ;
            //图片
            var imglis = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='post_box_img']/a/img", "src");
            if (imglis != null && imglis.Count > 0)
            {
                pro.ImgUrl = imglis[0];
            }
            //数据源
            var source = CustomMethod.GetValue(doc, "//span[@class='right_align']/a");
            pro.Source = source;
            //链接
            var outlink = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='post_box_go']/a[@rel='nofollow']", "href");
            if (outlink != null && outlink.Count > 0)
            {
                string productUrl = "";
                string link = outlink[0];
                //商品链接 带渠道号
                pro.Plink = CustomMethod.GetTrueUrl(link, ref productUrl);

                //入库前匹配数据库是否存在该商品 商品来源是否重复标识
                pro.Memo = productUrl;
            }
            else
            {
                return null;
            }
            //详细信息
            var detaillink = CustomMethod.GetAttr(doc, htmlContent, "//h3/a", "href");
            if (detaillink != null && detaillink.Count > 0)
            {
                var detail = detaillink[0];
                string htmlDescription = CustomMethod.GetHtmlByHttpHelper(detail);
                doc.LoadHtml(htmlDescription);//详细内容 

                //判断plink
                if (pro.Plink == "")
                {
                    var urlColl = CustomMethod.GetValue(doc, "//a[@class='clink deal_click']");
                    if (urlColl != "")
                    {
                        string productUrl = "";
                        if (urlColl.Contains("yhd"))
                        {
                            var newurl = "http://" + urlColl.Replace("&#8230;", "") + "html";
                            pro.Plink = jutehui.GetData.MallsEnumClass.GetSourceUrl(newurl, ref productUrl);
                            if (pro.Plink == "")
                            {
                                if (productUrl == "")
                                {
                                    CustomMethod.WriteLog("值值值：没有抓取到商品链接");
                                }
                                return null;
                            }
                            pro.Memo = productUrl;
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                var item = doc.DocumentNode.SelectNodes("//div[@class='content_a']/p");
                string descr = "";
                if (item != null && item.Count > 0)
                {
                    //for (int index = 0; index < item.Count; index++)
                    //{
                    string des = item[0].InnerText;
                    if (des.Contains("购买网址："))
                    {
                        pro.Description = pro.Pname;
                    }
                    else
                    {
                        descr += "<p>" + des + "</p><br/>";
                    }
                    //  }
                }
                pro.Description = descr;
                if (descr == "")
                {
                    pro.Description = pro.Pname;
                }
                ////大图
                //var bigTu = CustomMethod.GetAttr(doc, htmlDescription, "//img[@itemprop='image']", "src");

                //if (bigTu != null && bigTu.Count > 0)
                //{
                //    pro.ImgUrl2 = bigTu[0];
                //}
                //分类  
                doc.LoadHtml(htmlDescription);
                var conllectin = doc.DocumentNode.SelectNodes("//li/a[@rel='category tag']");//".//a[@href]"
                if (conllectin != null)
                {
                    try
                    {
                        //doc.LoadHtml(conllectin[0].InnerHtml);
                        //var aStr = doc.DocumentNode.SelectNodes("//a");//".//a[@href]"
                        string typstr = conllectin[0].InnerHtml;
                        var tyid = ProductTypeEnum.GetProductType(typstr, typeof(ProductTypeEnum.zhizhizhiType));
                        pro.ProductTypeId = tyid;
                    }
                    catch
                    {
                        pro.ProductTypeId = 16;
                    }
                }
                else
                {
                    pro.ProductTypeId = 16;
                }
            }
            return pro;
        }
    }

}
